package cn.itcast.bos.web.action.base;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.domain.base.SubArea;
import cn.itcast.bos.service.base.SubAreaService;
import cn.itcast.bos.web.action.common.BaseAction;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class SubAreaAction extends BaseAction<SubArea> {

	@Autowired
	private SubAreaService subAreaService;
	 private File file;

		public void setFile(File file) {
			this.file = file;
		}
	@Action(value="subarea_uploadfile")
	public String upload_file() throws Exception{
		List<SubArea> subAreas=new ArrayList<SubArea>();
		
		HSSFWorkbook hssfWorkbook=new HSSFWorkbook(new FileInputStream(file));
		HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
		for (Row row : sheet) {
			if(row.getRowNum()==0){
				continue;
			}
			if(row.getCell(0)==null || StringUtils.isBlank(row.getCell(0).getStringCellValue())){
				continue;
			}

			SubArea subArea=new SubArea();
			
			subArea.setId(row.getCell(0).getStringCellValue());
			subArea.setKeyWords(row.getCell(3).getStringCellValue());
			subArea.setStartNum(row.getCell(4).getStringCellValue());
			subArea.setEndNum(row.getCell(5).getStringCellValue());
			subArea.setSingle(row.getCell(6).getStringCellValue());
			subArea.setAssistKeyWords(row.getCell(7).getStringCellValue());
			subAreas.add(subArea);
			
		}
		subAreaService.save(subAreas);
		return NONE;
	}
	
	@Action(value="subarea_save",results={@Result(name="success",type="redirect",location="./pages/base/sub_area.html")})

	public String save(){
		
		subAreaService.save(model);
		
		return SUCCESS;
	}
	
	@Action(value="subarea_pagesearch",results={@Result(name="success" ,type="json")})
	public String pagesearch(){
		
		Pageable pageable=new PageRequest(page-1, rows);
		
		Specification<SubArea> specification=new Specification<SubArea>() {

			@Override
			public Predicate toPredicate(Root<SubArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate=null;
				List<Predicate> predicates=new ArrayList<Predicate>();
				
				if(StringUtils.isNoneBlank(model.getKeyWords())){
					Predicate p5 = cb.equal(root.get("keyWords").as(String.class), model.getKeyWords());
					predicates.add(p5);
				}
				Join<SubArea, Area> AreaJoin=root.join("area",JoinType.INNER);
				if(model.getArea()!=null
						&&  StringUtils.isNoneBlank(model.getArea().getProvince())){
					Predicate p1 = cb.like(AreaJoin.get("province").as(String.class),"%"+model.getArea().getProvince()+"%" );
					predicates.add(p1);
				}
				if(model.getArea()!=null
						&&  StringUtils.isNoneBlank(model.getArea().getCity())){
					Predicate p2 = cb.like(AreaJoin.get("city").as(String.class),"%"+model.getArea().getCity()+"%" );
					predicates.add(p2);
				}
				if(model.getArea()!=null
						&&  StringUtils.isNoneBlank(model.getArea().getDistrict())){
					Predicate p3= cb.like(AreaJoin.get("district").as(String.class),  "%"+model.getArea().getDistrict()+"%" );
					predicates.add(p3);
				}
				
				
				Join<SubArea, FixedArea> fixedAreaJoin=root.join("fixedArea",JoinType.INNER);
				if(model.getFixedArea()!=null
						&&  StringUtils.isNoneBlank(model.getFixedArea().getId())){
					Predicate p4 = cb.equal(fixedAreaJoin.get("id").as(String.class),model.getFixedArea().getId() );
					predicates.add(p4);
				}
				predicate=cb.and((Predicate[]) predicates.toArray(new Predicate[0]));
				
				return predicate;
			}
		};
		
		Page<SubArea>pageData=subAreaService.findAll(specification,pageable);
		
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}
}
